package br.com.bean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.ViewAccessScoped;
import org.primefaces.event.SelectEvent;

import br.com.domain.Menu;
import br.com.domain.MenuItem;
import br.com.domain.Perfil;
import br.com.domain.PerfilMenu;
import br.com.exception.BeanException;
import br.com.exception.ServiceException;
import br.com.filter.PerfilFilter;
import br.com.service.GenericServiceImpl;
import br.com.service.PerfilService;
import br.com.util.DomainListDataModel;

@Named
@SessionScoped
public class PerfilBean extends GenericBean{

	private static final long serialVersionUID = 1L;
	
	/**************************************************************************/
	/**                                ATRIBUTOS                             **/
	/**************************************************************************/
	
	@Inject /** Service **/	 
	private PerfilService perfilService; 
	
	/** Constante Injetado **/
	@Inject
	private MenuBean menuBean;
	
	/** Objeto Principal do Bean **/
	private Perfil perfil = new Perfil();	
	
	/** Lista Principal do Bean **/
	private DomainListDataModel listPerfil;
	
	/** Objeto de Filtro do Bean **/
	private PerfilFilter perfilFilter = new PerfilFilter();	  
	
	
	/**************************************************************************/
	/**                                M�TODOS                               **/
	/**************************************************************************/
	
	/** LOAD PAGE **/
	public void loadPage() throws ServiceException {
	
	}
	
	/** NOVO **/	
	public  void novo()  {
		
		try {
			this.activeModoNovo();
			/** NOVO PERFIL **/
			perfil = new Perfil();	
			
			/** INCIALIZANDO MENUS COM DEFAULT **/
			menuBean.getMenuPorPerfil(perfil);
			
			super.goPage("pagePerfilCad");
		
		} catch (Exception e) {
			MessageBean.showAlertMessage( e.getMessage());
		}
	}
	
	/** SALVAR **/
	public  void salvar()  {	
		
		try {
			
			if (!validarDomain(this.perfil)) return;
		
			/** LIMPANDO MENU ATUAL **/
			perfil.getListPerfilMenu().clear();
			
			/** VARRENDO MENU **/
			for (Menu menu : menuBean.getListMenuPerfil()) {
	
				/** VARRENDO MENU ITENS **/
				for (MenuItem menuItem : menu.getListMenuItem()) {
					
					PerfilMenu perfilMenu = new PerfilMenu();
					
					/** ITENS DE MENU **/
					perfilMenu.setIdMenu(menu.getId());	
					perfilMenu.setIdMenuItem(menuItem.getId());
					perfilMenu.setNomeMenuItem(menuItem.getNome());
					perfilMenu.setIncluir(menuItem.getIncluir());
					perfilMenu.setAlterar(menuItem.getAlterar());
					perfilMenu.setExcluir(menuItem.getExcluir());
					perfilMenu.setVisivel(menuItem.getVisivel());
					
					if (!menuItem.getTransacional()) {
						perfilMenu.setVisivel(menuItem.getVisivelNaoTransacional());
					}
					
					perfilMenu.setPerfil(perfil);
					
					/** ADICIONANDO LISTA DE MENU AO NOVO PERFIL**/
					perfil.getListPerfilMenu().add(perfilMenu);
				}
				
			}
			
			/** EXCLUINDO PERFIL MENU ATUAL **/
			perfilService.excluirPorPerfil(perfil);
			
			/** SALVANDO PERFIL **/
			perfilService.salvar(perfil);
			
			MessageBean.showInfoMessagePorChave("msg-info-operacao-sucesso");
		
            this.buscarTodos();
			
			this.goPage("pagePerfilList");
		
		} catch (ServiceException e) {
			MessageBean.showAlertMessage(e.getMessage());
		}
	}
	
	/** EDITAR **/
	public  void editar() {
		try {
			this.activeModoEditar();
			menuBean.getMenuPorPerfil(perfil);		
			this.goPage("pagePerfilCad");
		
		} catch (Exception e) {
			MessageBean.showAlertMessage(e.getMessage());
		}
	}
	
	/** EXCLUIR **/
	public  void excluir() {
		try{
			
			this.perfilService.excluir(this.perfil);
			MessageBean.showInfoMessagePorChave("msg-info-operacao-sucesso");
			this.buscarTodos();
			this.goPage("pagePerfilList");
		
		} catch (Exception e) {
			MessageBean.showAlertMessage(e.getMessage());
		}
	}
	
	
	/** BUSCAR TODOS **/
	public DomainListDataModel buscarTodos() {
		try{
			
		  this.listPerfil =  this.buscarTodos(Perfil.class);
		  
		} catch (Exception e) {
			MessageBean.showAlertMessage(e.getMessage());
		}
		return this.listPerfil;
	}
	
	
	
	/** BUSCAR POR FILTRO **/
	public void buscarPorFiltro()  {
		try {
			this.listPerfil = buscarPorFiltro(this.perfilFilter);
		} catch (BeanException e) {
			MessageBean.showErrorMessage(e.getMessage());
		}
	}
	
	/** VOLTAR **/
	public void voltar() throws ServiceException {
	   this.goPage("pagePerfilList");
	}
	
	/**************************************************************************/
	/**                      METODOS  COMPLEMENTEARES                        **/
	/**************************************************************************/
	
	 @PostConstruct	 
	 public void init() throws ServiceException{
	    buscarTodos();
	    System.out.println("Bean Perfil Iniciado");
	 }
	 
	 /**
		 * LINHA SELECIONADA
		 * @return void
		 * @throws ServiceException 
		 */
		public void onRowSelect(SelectEvent  event) throws ServiceException {
			this.perfil = (Perfil) event.getObject();
			this.editar();		
		}
		
		 @PreDestroy
		 public void finalizarBean(){
		    
		    System.out.println("Bean Perfil Destruido");
		 }

		
	/**************************************************************************/
	/**                           GET's E SET's                              **/
	/**************************************************************************/
	@Override
	public void setGenericService(GenericServiceImpl genericService) {
		super.setGenericService(this.perfilService);
	}

	@Override
	public GenericServiceImpl getGenericService() {
		return this.perfilService;
	}
	@Override
	public DomainListDataModel getListDomain() {
		return this.getListPerfil();
	}
	
	public Perfil getPerfil() {
		return perfil;
	}

	public void setPerfil(Perfil perfil) {
		this.perfil = perfil;
	}

	
	public DomainListDataModel getListPerfil() {
		return listPerfil;
	}

	public void setListPerfil(DomainListDataModel listPerfil) {
		this.listPerfil = listPerfil;
	}


	public PerfilService getPerfilService() {
		return perfilService;
	}

	public void setPerfilService(PerfilService perfilService) {
		this.perfilService = perfilService;
	}

	public MenuBean getMenuBean() {
		return menuBean;
	}

	public void setMenuBean(MenuBean menuBean) {
		this.menuBean = menuBean;
	}

	public PerfilFilter getPerfilFilter() {
		return perfilFilter;
	}

	public void setPerfilFilter(PerfilFilter perfilFilter) {
		this.perfilFilter = perfilFilter;
	}
	
	
	
}
